<template>
  <div class="tickets-list">
    <!-- 首推  -->
    <bigTicketItem :item="landmarkTicket" @click="handleClick" :showDistance="ticketParams.isCurrentCity"/>
    <basic-list @load="loadMore" :isFinished="isFinished" :isLoading="isLoading" :isEmpty="!isLoading&&tickets.length == 0">
      <ticket-item  v-for="item in tickets" :key="item.id" :item="item" :showDistance="ticketParams.isCurrentCity" @click="handleClick(item)"/>
    </basic-list>
  </div>
</template>
<script>
import sdk from 'sdk'
import ticketItem from '@/components/business/product-item/ticket-item'
import sleep from '@/utils/helper/sleep'
import {encodeQuery} from 'utils/helper/query'
import { searchTickets } from '@/api/index'
import bigTicketItem from '@/components/business/product-item/big-ticket-item'

export default {
  name: 'tickets-list',
  components: {
    ticketItem, bigTicketItem
  },

  props: {
    params: { type: Object, default: () => {
        return {}
      } 
    }
  },

  data() {
    return {
      tickets: [],
      landmarkTicket: {},
      isFinished: false,
      isLoading: false,
      ticketParams: {}
    }
  },
  watch:{
    params: {
      handler: function(val){
        if(!val.name) return
        this.isFinished = false
        this.ticketParams = { ...val }
        this.tickets = []
        this.loadMore()
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    loadTickects() {
      if(!this.ticketParams.pageSize ||this.isFinished) return
      this.isLoading = true
      // console.log('门票列表页查询参数：', this.ticketParams)
      searchTickets(this.ticketParams).then(res=>{
        this.isLoading = false
        this.landmarkTicket = res.result.landmarkTicketInfo
        // this.tickets = this.tickets.concat(res.result.ticketInfos)
        
        if(this.ticketParams.pageNum <= 1) {
          this.tickets = res.result.ticketInfos
        } else {
          this.tickets = this.tickets.concat(res.result.ticketInfos)
        }

        if(!res.result.ticketInfos || !res.result.ticketInfos.length || res.result.ticketInfos.length < this.ticketParams.pageSize) {
          this.isFinished = true
        }

        this.ticketParams.pageNum = this.ticketParams.pageNum + 1
      })
    },
    async loadMore() {
      if (this.isLoading || this.isFinished) return 
      this.loadTickects()
    },

    /**
     * 点击列表项
     * @param {Object} item 列表项对象 
     */
    handleClick(item) {
      let path = ''
      let queryStr = encodeQuery({
        code: item.productCode,
        authorization:''
      })
      sdk.navigateTo({
        url: `/packageDetail/pages/tickets/detail/index?${queryStr}`
        // url: `/ubd/product/poi/detail?${queryStr}`
      })
    }
  }
}
</script>
<style lang="less" scoped>

</style>